<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace addons\gs_developer;

use addons\gs_developer\model\ExceptionModel;
use EasyWeChat\Kernel\Messages\Message;
use hiduoke\event\BaseEvent;
use hiduoke\services\FormBuilder as Form;
use think\facade\Log;

class Listen extends BaseEvent
{
    //    public $name = 'gs_developer';

    /**
     * 接收微信公众平台支付响应
     * @param $param
     * @return bool
     */
    public function demo_wx_wechat_notify($param)
    {
        Log::write(['demo_wx_wechat_notify' => $param]);
        return true;
    }

    /**
     * 微信通讯消息
     * @param $message
     * @return Message|false
     */
    public function wechat_message($message)
    {
        addon_event('wechat_message', "微信通讯消息", $message);
        return '';
    }

    /**
     * 企业微信客服事件消息
     * @param $data
     */
    public function wework_kf_message($data)
    {
        addon_event('wework_kf_message', "企业微信客服事件消息", $data);
    }

    /**
     * 企业微信客户联系事件消息
     * @param $data
     */
    public function wework_customer_message($data)
    {
        addon_event('wework_customer_message', "企业微信客户联系", $data);
    }

    public function wework_event_message($data)
    {
        addon_event('wework_event_message', "企业微信事件消息（包含第三方）", $data);
    }

    public function delete_platform($plaid)
    {
        addon_event('delete_platform', "删除平台", $plaid);
    }

    //     /**
    //      * 增加客户端菜单
    //      */
    public function customer_menu_hook()
    {
        return [
            [
                'title' => '开发者工具',
                // 'url'   => '/gs_developer/#/home',
                'url' => 'http://www.dkyx.cc',
                //                                'url'   => 'http://localhost:7200',
                'name'  => 'gs_developer',
                'type'  => 'link',
                'target'=>'_blank'
            ],
        ];
    }


    // ========== 页面头部 Hooks ==========

    // 在头部用户信息左侧显示内容
    public function header_user_left_hook()
    {
        return '<a style="color:#165DFF;margin-right: 10px;font-size:14px;font-weight: bold" href="/gs_developer" target="_blank">🚀开发工具</a>';
    }

    // 在头部用户信息右侧显示内容
    public function header_user_right_hook()
    {
        $content = '';

        // 显示当前时间
        $content .= '<span class="hook-time" style="font-size:14px; margin-left:14px;">' . date('H:i') . '</span>';

        return $content;
    }


    //    /**
    //     * 导航右侧-用户头像左侧-嵌入点
    //     * @return false|mixed|string
    //     * @throws \think\Exception
    //     */
    //    public function header_user_left_hook()
    //    {
    //        return $this->fetch();
    //    }

    /**
     * 插件配置
     * @param $row
     * @return array
     */
    public function gs_developer_addon_config($row)
    {
        $rule = array();
        $rule[] = Form::input('dev_id', '开发者ID', $row['dev_id'] ?? '')->required();
        $rule[] = Form::input('token', '通讯秘钥', $row['token'] ?? '')->required();
        // 事件监听多选
        $rule[] = Form::checkbox('event_listen', '监听事件', $row['event_listen'] ?? [])
            ->options([
                ['label' => '应用初始化标签位(AppInit)', 'value' => 'AppInit', 'disabled' => true],
                ['label' => '应用开始标签位(HttpRun)', 'value' => 'HttpRun', 'disabled' => true],
                ['label' => '路由加载完成(RouteLoaded)', 'value' => 'RouteLoaded', 'disabled' => true],
                ['label' => '写入日志时候调用(LogWrite)', 'value' => 'LogWrite', 'disabled' => true],
                ['label' => '日志记录时调用(LogRecord)', 'value' => 'LogRecord', 'disabled' => true],
                ['label' => '应用路由开始前执行(xxx_addon_begin)', 'value' => '_addon_begin', 'disabled' => true],
                ['label' => '应用路由开始后执行(xxx_addon_end)', 'value' => '_addon_end', 'disabled' => true],
                ['label' => '应用全局中间件(addon_middleware)', 'value' => 'addon_middleware', 'disabled' => true],
                ['label' => '微信通讯消息(wechat_message)', 'value' => 'wechat_message'],
                ['label' => '企业微信客服事件消息(wework_kf_message)', 'value' => 'wework_kf_message'],
                ['label' => '企业微信客户联系事件消息(wework_customer_message)', 'value' => 'wework_customer_message'],
            ])->effect(['help' => '选择要进行记录的事件，方便开发调试，部分事件无法选择，如：应用初始化标签位(AppInit、应用开始标签位(HttpRun)...']);
        return $rule;
    }

    //    /**
    //     * 系统错误日志记录
    //     * @param $e
    //     * @return void
    //     */
    //    public function ExceptionHandle($e)
    //    {
    //        (new ExceptionModel())->crateException($e);
    //    }
}
